package com.imooc.jdbc.smaple;

import java.sql.*;

public class StandardJDBCSample {

    //psvm

    public static void main(String[] args) {

        Connection conn = null;
        Statement stmt = null;

        try{

        // 1.加载并注册JDBC驱动
        Class.forName("com.mysql.cj.jdbc.Driver");

        // 2.创建数据库连接
            //jdbc:mysql://192.168.7.190:3306/yulei_test?useSSL=false&useUnicode=true&characterEncoding=UTF-8
        conn = DriverManager.getConnection(
                "jdbc:mysql://192.168.7.190:3306/yulei_test?useSSL=false&characterEncoding=UTF-8&useUnicode=true",
                "testreadwrite",
                "testrw190!@#");

        // 3. 创建Statement对象
        stmt = conn.createStatement();

        ResultSet rs = stmt.executeQuery("select * from ay_user_address; ");

        // 4.遍历查询结果
        while(rs.next()){
            Integer id = rs.getInt(1);
            String name = rs.getString(2);

            // sout
            System.out.println("id: " + id + ", name: " + name);

           }
        }catch (Exception ex){
            ex.printStackTrace();
        }finally {

            // 5. 关闭连接，释放资源
            try {
                if(conn != null && !conn.isClosed()){
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
